package cn.edu.neusoft.wuguangsheng.flowerimpression.activity;

import android.graphics.Color;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;

import java.util.ArrayList;

import cn.edu.neusoft.wuguangsheng.flowerimpression.R;
import cn.edu.neusoft.wuguangsheng.flowerimpression.adapter.MyFragmentPagerAdapter;
import cn.edu.neusoft.wuguangsheng.flowerimpression.fragment.OrderFiveFragment;
import cn.edu.neusoft.wuguangsheng.flowerimpression.fragment.OrderFourFragment;
import cn.edu.neusoft.wuguangsheng.flowerimpression.fragment.OrderOneFragment;
import cn.edu.neusoft.wuguangsheng.flowerimpression.fragment.OrderThreeFragment;
import cn.edu.neusoft.wuguangsheng.flowerimpression.fragment.OrderTwoFragment;

public class OrderViewActivity extends BaseActivity implements View.OnClickListener,ViewPager.OnPageChangeListener {

    private ViewPager myviewpager1;
    private ArrayList<Fragment> fragments;
    ImageView myorderview_fh;

    //选项卡中的按钮
    private Button btn_first;
    private Button btn_second;
    private Button btn_third;
    private Button btn_four;
    private Button btn_fifth;
    //作为指示标签的按钮
    private ImageView cursor1;
    //标志指示标签的横坐标
    float cursorX = 0;
    //所有按钮的宽度的数组
    private int[] widthArgs;
    //所有标题按钮的数组
    private Button[] btnArgs;
    @Override
    void initView() {
        setLayout_file(R.layout.activity_order_view);
        myorderview_fh = (ImageView) findViewById(R.id.myorderview_fh);
        initViews();
    }

    @Override
    void initEvent() {
        myorderview_fh.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });

    }

    @Override
    void initDate() {

    }

    //初始化布局
    public void initViews(){
        myviewpager1 = (ViewPager)findViewById(R.id.myviewpager1);
        btn_first = (Button)findViewById(R.id.btn_1);
        btn_second = (Button)findViewById(R.id.btn_2);
        btn_third = (Button)findViewById(R.id.btn_3);
        btn_four = (Button)findViewById(R.id.btn_4);
        btn_fifth = (Button)findViewById(R.id.btn_5);
        //初始化按钮数组
        btnArgs = new Button[]{btn_first,btn_second,btn_third,btn_four,btn_fifth};
        //指示标签设置为红色
        cursor1 = (ImageView)findViewById(R.id.cursor_btn1);
        cursor1.setBackgroundColor(Color.RED);

        btn_first.setOnClickListener(this);
        btn_second.setOnClickListener(this);
        btn_third.setOnClickListener(this);
        btn_four.setOnClickListener(this);
        btn_fifth.setOnClickListener(this);




        myviewpager1.setOnPageChangeListener(this);

        fragments = new ArrayList<Fragment>();
        fragments.add(new OrderOneFragment());
        fragments.add(new OrderTwoFragment());
        fragments.add(new OrderThreeFragment());
        fragments.add(new OrderFourFragment());
        fragments.add(new OrderFiveFragment());


        MyFragmentPagerAdapter adapter = new MyFragmentPagerAdapter(OrderViewActivity.this.getSupportFragmentManager(),fragments);
        myviewpager1.setAdapter(adapter);

        //先重置所有按钮颜色
       resetButtonColor();
        //再将第一个按钮字体设置为红色，表示默认选中第一个
        btn_first.setTextColor(Color.RED);


        btnArgs = new Button[]{btn_first,btn_second,btn_third,btn_four,btn_fifth};
        btn_first.post(new Runnable(){
            @Override
            public void run() {
                LinearLayout.LayoutParams lp = (LinearLayout.LayoutParams)cursor1.getLayoutParams();
                //减去边距*2，以对齐标题栏文字
                lp.width = btn_first.getWidth()-btn_first.getPaddingLeft()*2;
                cursor1.setLayoutParams(lp);
                cursor1.setX(btn_first.getPaddingLeft());
            }
        });

    }
    //重置所有按钮的颜色
    public void resetButtonColor(){
        btn_first.setBackgroundColor(Color.parseColor("#FFFFFF"));
        btn_second.setBackgroundColor(Color.parseColor("#FFFFFF"));
        btn_third.setBackgroundColor(Color.parseColor("#FFFFFF"));
        btn_four.setBackgroundColor(Color.parseColor("#FFFFFF"));
        btn_fifth.setBackgroundColor(Color.parseColor("#FFFFFF"));

        btn_first.setTextColor(Color.BLACK);
        btn_second.setTextColor(Color.BLACK);
        btn_third.setTextColor(Color.BLACK);
        btn_four.setTextColor(Color.BLACK);
        btn_fifth.setTextColor(Color.BLACK);
    }
    @Override
    public void onPageScrollStateChanged(int arg0) {
        // TODO Auto-generated method stub

    }

    @Override
    public void onPageScrolled(int arg0, float arg1, int arg2) {
        // TODO Auto-generated method stub

    }
    @Override
    public void onPageSelected(int arg0) {

        if (widthArgs == null){
            widthArgs = new int[]{btn_first.getWidth(),
                    btn_second.getWidth(),
                    btn_third.getWidth(),
                    btn_four.getWidth(),
                    btn_fifth.getWidth()
            };
        }
        // TODO Auto-generated method stub
        //每次滑动首先重置所有按钮的颜色
        resetButtonColor();
        //将滑动到的当前按钮颜色设置为红色
        btnArgs[arg0].setTextColor(Color.RED);
        cursorAnim(arg0);

    }
    //指示器的跳转，传入当前所处的页面的下标
    public void cursorAnim(int curItem){
        //每次调用，就将指示器的横坐标设置为0，即开始的位置
        cursorX = 0;
        //再根据当前的curItem来设置指示器的宽度
        LinearLayout.LayoutParams lp = (LinearLayout.LayoutParams)cursor1.getLayoutParams();
        //减去边距*2，以对齐标题栏文字
        lp.width = widthArgs[curItem]-btnArgs[0].getPaddingLeft()*2;
        cursor1.setLayoutParams(lp);
        //循环获取当前页之前的所有页面的宽度
        for(int i=0; i<curItem; i++){
            cursorX = cursorX + btnArgs[i].getWidth();
        }
        //再加上当前页面的左边距，即为指示器当前应处的位置
        cursor1.setX(cursorX+btnArgs[curItem].getPaddingLeft());
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.btn_1:
                myviewpager1.setCurrentItem(0);
                cursorAnim(0);
                break;
            case R.id.btn_2:
                myviewpager1.setCurrentItem(1);
                cursorAnim(1);
                break;
            case R.id.btn_3:
                myviewpager1.setCurrentItem(2);
                cursorAnim(2);
                break;
            case R.id.btn_4:
                myviewpager1.setCurrentItem(3);
                cursorAnim(3);
                break;
            case R.id.btn_5:
                myviewpager1.setCurrentItem(4);
                cursorAnim(4);
                break;



        }
    }
}
